package com.magic.smartcity.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.magic.smartcity.R;
import com.magic.smartcity.databinding.ActivityMainBinding;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private NavController mNavController;
    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //获得fragment视图
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_view);
        mNavController = navHostFragment.getNavController();
//        mNavController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_more, R.id.nav_news, R.id.nav_smartchina, R.id.nav_me)
                .build();


        //绑定acitonBar
        NavigationUI.setupActionBarWithNavController(this, mNavController, mAppBarConfiguration);

        //安装绑定导航栏和显示容器
        NavigationUI.setupWithNavController(binding.navView, mNavController);

        //获取标题栏
        ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.action_bar_item_search, null, false));

        //导航事件监听
        mNavController.addOnDestinationChangedListener((controller, destination, arguments) -> {
            switch (destination.getId()) {
                case R.id.loginFragment:
                case R.id.registerFragment:
                case R.id.homeHotTopicFragment:
                case R.id.newsDetailFragment:
                case R.id.metroHomeFragment:
                case R.id.metroCardFragment:
                case R.id.metroAboutFragment:
                case R.id.action_nav_me_to_editInfoFragment:
                case R.id.metroHomeLineDetailFragment:
                case R.id.metroHomeMapFragment:
                case R.id.activityHomeFragment:
                case R.id.activityDetailFragment:
                case R.id.hospitalDetailFragment:
                case R.id.hospitalPatientCardFragment:
                case R.id.hospitalPatientCardAddFragment:
                case R.id.hospitalPatientCardEditFragment:
                case R.id.hospitalDepartmentsFragment:
                case R.id.hospitalDepartmentsDetailFragment:
                case R.id.hospitalReservationDetailFragment:
                case R.id.parkHomeFragment:
                case R.id.parkDetailFragment:
                case R.id.parkRecordFragment:
                case R.id.busHomeFragment:
                case R.id.busDetailFragment:
                case R.id.busRidingTimeFragment:
                case R.id.busRidingPassengerFragment:
                case R.id.busSubmitOrderFragment:
                case R.id.busOrderFragment:
                    //隐藏底部导航栏 不显示搜索框
                    binding.navView.setVisibility(View.GONE);
                    actionBar.setDisplayShowCustomEnabled(false);
                    break;
                case R.id.hospitalFragment:
                    //只显示搜索框
                    binding.navView.setVisibility(View.GONE);
                    actionBar.setDisplayShowCustomEnabled(true);
                    break;
                case R.id.nav_home:
                case R.id.nav_more:
                case R.id.action_nav_home_to_nav_more:
                    //显示搜索框 显示导航栏
                    binding.navView.setVisibility(View.VISIBLE);
                    actionBar.setDisplayShowCustomEnabled(true);
                    break;
                default:
                    //默认全部显示(不显示搜索框)
                    binding.navView.setVisibility(View.VISIBLE);
                    actionBar.setDisplayShowCustomEnabled(false);
            }
        });
    }


    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(mNavController, mAppBarConfiguration) || onSupportNavigateUp();
    }

    public void setActionBarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

}